package com.wcar.product.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.GoodsImage;
import com.wcar.common.entity.User;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.product.service.impl.ProductImageServiceImpl;
import com.wcar.product.vo.ProductImageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/product/image")
public class ProductImageController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductImageServiceImpl productImageService;
    @PostMapping("/select")
    public Result<Page<GoodsImage>> select(@RequestBody ProductImageVo productImageVo) {
        return productImageService.select(productImageVo);
    }
    @PostMapping("/insertProductImage")
    public Result<GoodsImage> insertProductImage(@RequestBody GoodsImage goodsImage) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        goodsImage.setCreator(user.getUserId());
        goodsImage.setCreatetime(new Date());
        return productImageService.insertProductImage(goodsImage);
    }
    @PostMapping("/updateProductImage")
    public Result<GoodsImage> updateProductImage(@RequestBody GoodsImage goodsImage) {
        return productImageService.updateProductImage(goodsImage);
    }
    @GetMapping("/deleteProductImageById")
    public Result<GoodsImage> deleteProductImageById(@RequestParam Integer id) {
        return productImageService.deleteProductImageById(id);
    }
}
